package com.web.crawler;

import com.web.crawler.DocumentIndexer;
import com.web.crawler.ElasticSearchUtil;
import com.web.crawler.SearchService;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        try {
            // 建立索引
            System.out.println("Indexing English documents...");
            DocumentIndexer.indexDocuments("en");

            System.out.println("Indexing Chinese documents...");
            DocumentIndexer.indexDocuments("zh");

            // 示例搜索
            System.out.println("Searching for 'technology' in English...");
            List<Map<String, Object>> enResults = SearchService.search("technology", "en");
            enResults.forEach(result -> {
                System.out.println("Title: " + result.get("title"));
                System.out.println("Content snippet: " + ((String)result.get("content")).substring(0, 100) + "...");
                System.out.println("---------------------");
            });

            System.out.println("Searching for '科技' in Chinese...");
            List<Map<String, Object>> zhResults = SearchService.search("科技", "zh");
            zhResults.forEach(result -> {
                System.out.println("Title: " + result.get("title"));
                System.out.println("Content snippet: " + ((String)result.get("content")).substring(0, 100) + "...");
                System.out.println("---------------------");
            });

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ElasticSearchUtil.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}